<?php

/**
 * This class Mac_is used to manipulate session
 *
 * @author Mathieu Pheulpin - sokaru91@gmail.com
 */

class Mac_Session_Namespace
{
    /**
     * Name of the namespace
     * @var string
     */
    private $name;
    
    /**
     * Data in the session namespace
     * @var array
     */
    private $data;

    /**
     * Constructor of Session Namespace
     * @param string $name Name of the namespace
     */
    public function __construct($name)
    {
        $this->name = $name;
        if (!isset($_SESSION[$this->name])) {
            $this->data = array();
            $this->save();
        } else {
            $this->data = $_SESSION[$this->name];
        }
    }

    /**
     * Setter of a value in the namespace
     * @param string $name Value name
     * @param string $value Value
     */
    public function set ($name,  $value)
    {
        $this->data[$name] = $value;
        $this->save();
    }

    /**
     * Getter of a value in the namespace
     * @param string $name Value name
     */
    public function get ($name)
    {
        if (isset($_SESSION[$this->name][$name])) {
            return $_SESSION[$this->name][$name];
        }
        return null;
    }
    
    /**
     * Namespace saver, save in $_SESSION array
     */
    private function save ()
    {
        $_SESSION[$this->name] = $this->data;
    }

}
